Merge selected objects including links

Hello,

Trying to merge selected objects including links, just wondering if my logic is correct and I was wondering is there a better way of doing it? Thank you.

-Jim
 

//MergeSelectedObjects.dxl
 
/*
    Join selected objects on Object Text.
    Also move links from deleted objects to joined object.

    1. Select a set of contiguous objects.
        (Only their object text will be retained.)
    2. Run this script to replace the object text in
        the first of these objects with the concatenation
        of the object texts of the other objects, and then
        delete those other objects.
*/
 
#include <utils/linkops.dxl>
#include <example/include/dbs.inc>
pragma runLim, 0
 
void moveLinks(Object fromObj, Object toObj) 
{
    // move all incoming and outgoing links from
    // one object to another
 
    // scan outgoing links
    Skip deleteLINKS = create()
    Link l
    ModName_ otherMod = null
    ModuleVersion otherVersion = null
    Object othero = null
    const string linkModName = "*"
    for l in all(fromObj->linkModName) do 
        {
        otherVersion = targetVersion l
        otherMod = module(otherVersion)
        if (null otherMod || isDeleted otherMod) continue
        othero = target l
        if (null othero) {load(otherVersion,false)}
        othero = target l
        if (null othero) continue
        if (isDeleted othero) continue
                Error = linkErrorNoError   
                Error = createLink_(toObj,fullName(module(l)),othero)
                // show errors
                if (Error != linkErrorNoError) {reportLinkError_(toObj,othero,fullName(module(l)),"",Error)}
                else{put(deleteLINKS, l, l)}
    }
        
        for l in deleteLINKS do {delete l}
 
    // scan incoming links
    LinkRef lr
    otherMod = null
    otherVersion = null
        othero = null
    for lr in all(fromObj<-linkModName) do 
        {
        otherMod = module (sourceVersion lr)
        if (!null otherMod) 
                {if ((!isDeleted otherMod) && (null data(sourceVersion lr))) {load((sourceVersion lr),false)}}
    }
    for l in all(fromObj<-linkModName) do 
        {
        otherVersion = sourceVersion l
        otherMod = module(otherVersion)
        if (null otherMod || isDeleted otherMod) continue
        othero = source l
        if (null othero) {load(otherVersion,false)}
        othero = source l
        if (null othero) continue
        if (isDeleted othero) continue
                Module inlink = edit(fullName(module(othero)),true)
                if (!null inlink)
                {
                        Error = linkErrorNoError   
                        Error = createLink_(othero,fullName(module(l)),toObj)
                        // show errors
                        if (Error != linkErrorNoError) {reportLinkError_(othero,toObj,fullName(module(l)),"",Error)}
                        else{put(deleteLINKS, l, l)}
                }else {ack "{ Null parameter} "fullName(module(othero))}
    }
        
        for l in deleteLINKS do {delete l}
        delete deleteLINKS
    flushDeletions()
}
 
 
Skip   toDelete = create()
Object firstObj = null
string objText  = ""
int    cellWdth = 0
int    numObjs  = 0
 
Object o
for o in current Module do
{
        if (!isSelected o) continue
 
        // accumlate object texts
        objText = objText "\n" (richTextWithOle o."Object Text") ""
 
    // accumulate cell widths
        cellWdth += intOf o."TableCellWidth" ""
 
        if (null firstObj) {firstObj = o} // record first object 
        else {put(toDelete, ++numObjs, o)} // record which objects to delete
}
 
if (null firstObj || numObjs < 1) halt
 
// update first object
firstObj."Object Text" = richText objText[1:] // cut off leading "\n"
if (cell(firstObj)) setCellWidth(firstObj, cellWdth)
 
// copy links
// delete remaining objects
// in reverse order so that children are deleted before parents
for i in numObjs:1 by -1 do
{
    if (find(toDelete, i, o))
        {
                        moveLinks(o, firstObj)
                        delete(o)
        }
}
flushDeletions()
delete toDelete

SystemAdmin - Mon Nov 12 14:44:18 EST 2012